<?php
global $AppUI, $m, $tab;

$uid = $AppUI->user_id;
// Check permission
// TODO: Check permission role
// Check edit
$perms =& $AppUI->acl();
$canAccess = $perms->checkModuleItem($m, 'access');
$canEdit = $perms->checkModuleItem($m, 'delete');
// check permission list quotations
if(!$canAccess){
	$AppUI->redirect(PUBLIC_PAGE);
}

require_once($AppUI->getModuleClass('companies'));
require_once($AppUI->getModuleClass('suppliers'));
require_once($AppUI->getMasterDataClass('mdcurrency'));

require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$types = dPgetSysVal( 'CompanyType' );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 10));

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('COMPANY_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('COMPANY_POST');
	}
	else {
		$_POST = $AppUI->getPOST('COMPANY_POST');
	}
}

$company_status = intval( dPgetParam( $_POST, 'company_status', 0 ) );
$company_type2 = intval( dPgetParam( $_POST, 'company_type2', 0 ) );
$company_name = dPgetParam( $_POST, 'company_name', '' );
$company_code = dPgetParam( $_POST, 'company_code', '' );
$keyword = dPgetParam( $_POST, 'keyword', '' );
$company_ledge1 = dPgetParam( $_POST, 'company_ledge1', '' );
$company_ledge2 = dPgetParam( $_POST, 'company_ledge2', '' );
$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
$tab = intval( dPgetParam( $_GET, 'tab', 0 ) );
//$flagResult = 0;

$where = " 1=1";

if ($company_status != 0) {/*$flagResult = -1;*/ $where .= " AND company_status = $company_status";}
if ($company_type2 != "") {/*$flagResult = -1;*/$where .= " AND company_type2 = $company_type2";}
if ($company_ledge1 != "") {/*$flagResult = -1;*/$where .= " AND company_ledge1 LIKE '%$company_ledge1%'";}
if ($company_ledge2 != "") {/*$flagResult = -1;*/$where .= " AND company_ledge2 LIKE '%$company_ledge2%'";}
if ($company_name != "") {/*$flagResult = -1;*/$where .= " AND company_name LIKE '%$company_name%'";}
if ($company_code != "") {/*$flagResult = -1;*/$where .= " AND company_code LIKE '%$company_code%'";}
if ($keyword != "") {/*$flagResult = -1;*/$where .= " AND (company_code LIKE '%$keyword%' OR company_name LIKE '%$keyword%')";}

// Load Object Status
$objMD = new CMdStatus();
$statList = $objMD->getStatusList('Customer');
$statList = arrayMerge(array('0'=>''), $statList);

$objMD = new CMdType();
$typeList = $objMD->getTypeList('Customer');
$typeList = arrayMerge(array('' => ''), $typeList);
global $m;
?>
<form name="searchFrm" action="./index.php?m=<?=$m?>&tab=<?=$tab?>" method="post">
<input type="hidden" name="offset">
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="infopanel">
<tr>
	<td>
		<input class="button" type="submit" name="searchAction" value="<?=$AppUI->_('search');?>"/>
		<input class="button" type="submit" name="resetAction" value="<?=$AppUI->_('reset');?>"/>
	</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="subinfopanel">
<tr>
	<td align="right"><?=$AppUI->_('Code')?></td>
	<td>
		<input type="text" class="text" name="company_code" id="company_code" value="<?=$company_code?>" maxlength="50"/>
	</td>
	<td align="right"> <?=$AppUI->_('Status')?></td>
	<td width="80%">
		<?= arraySelect($statList, 'company_status', ' id="company_status" size="1" class="text" style="width: 135px;"', $company_status, true)?>
	</td>
</tr>
<tr>
	<td nowrap="nowrap" align="right"><?=$AppUI->_('Name')?></td>
	<td>
		<input type="text" class="text" name="company_name" id="company_name" value="<?=$company_name?>" maxlength="50"/>
	</td>
	<td align="right"><?=$AppUI->_('Type')?></td>
	<td><?= arraySelect($typeList, 'company_type2', ' id="company_type2" size="1" class="text" style="width: 135px;"', $company_type2, true)?></td>
</tr>
<tr>
	<td align="right" nowrap="nowrap"><?=$AppUI->_('Ledge 1')?></td>
	<td>
		<input type="text" class="text" name="company_ledge1" id="company_ledge1" value="<?=$company_ledge1?>" maxlength="50"/>
	</td>
	<td align="right" nowrap="nowrap"><?=$AppUI->_('Ledge 2')?></td>
	<td>
		<input type="text" class="text" name="company_ledge2" id="company_ledge2" value="<?=$company_ledge2?>" maxlength="50" style="width: 129px;"/>
	</td>
</tr>
</table>
</form>
<?php

//echo $where;

//if ($flagResult < 0){
	// paging

	$q  = new DBQuery;
	$q->addTable('companies', 'c');
	//$q->addQuery('company_id, company_code, company_name, company_status, company_type, company_ledge1, company_ledge2, s.status_name, t.name as type_name');
	$q->addQuery('company_id, company_code,company_exact_code, company_name, company_address1, company_phone1, company_fax, company_vat_code, div_name');
	$q->addJoin('md_status', 's', 's.status_id = c.company_status');
	$q->addJoin('md_type', 't', 't.type_id = c.company_type2');
	$q->addJoin('divisions', 'd', 'd.div_id = c.company_division');
	$q->addWhere($where);
	$qTotal = $q->prepare();
//	echo $qTotal;
	$q->setLimit($limit,$offset);
	$objResult = $q->loadList();

	$none = true;
	$totalRows = 0;
	$qTotal = str_ireplace('select *', 'select count(*) as total_rows', $qTotal);
	$tmp = db_loadList($qTotal);
	$totalRows = $tmp[0]['total_rows'];

	require_once($AppUI->getLibraryClass('codeigniter/pagination'));
	$pagination = new CI_Pagination();
	$config = array(
		'base_url' => '?m=companies&tab=1',
		'total_rows' => $totalRows,
		'per_page' => $page_limit

	);
	$pagination->initialize($config);
	$i = 0;
	?>
	<script type="text/javascript">
		function submitForm(offset) {
			document.searchFrm.offset.value = offset;
			document.searchFrm.submit();
		}
	</script>
	<table id="idCompanyList" cellspacing="0" cellpadding="2" border="0" width="100%" class="list">
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<th ><?=$AppUI->_('Code')?></th>
			<th ><?=$AppUI->_('Exact code')?></th>
			<th ><?=$AppUI->_('Name')?></th>
			<th ><?=$AppUI->_('Address')?></th>
			<th ><?=$AppUI->_('Tel')?></th>
			<th ><?=$AppUI->_('Fax')?></th>
			<th ><?=$AppUI->_('VAT code')?></th>
			<th ><?=$AppUI->_('Division')?></th>
			<?php if ($canEdit): ?>
			<th ><?=$AppUI->_('Delete')?></th>
			<?php endif;?>
			<!--th ><?=$AppUI->_('Status')?></th>
			<th ><?=$AppUI->_('Type')?></th>
			<th ><?=$AppUI->_('Ledge 1')?></th>
			<th ><?=$AppUI->_('Ledge 2')?></th-->
		</tr>
	<?
	//showFVar($objResult);
	foreach ($objResult as $row) {
		$i++;
		$none = false;
		?>
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>" >
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_code']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_exact_code']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_name']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_address1']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_phone1']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_fax']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['company_vat_code']?></td>
			<td onclick="showCompany(<?=$row['company_id']?>)"><?=$row['div_name']?></td>
			<?php if ($canEdit) {
			echo "<td align='center'>";
				showIconLink('images/delete.gif', 'js:doDelete('.$row["company_id"]. ', \''.addslashes($row["company_name"]).'\')', 'Delete');
			echo "</td>";
			}
			?>
			<!--td ><?=$row['status_name']?></td>
			<td ><?=$row['type_name']?></td>
			<td ><?=$row['company_ledge1']?></td>
			<td ><?=$row['company_ledge2']?></td-->
		</tr>
		<?
	}
	if ($none) {
	echo '<tr><td colspan="100%">' . $AppUI->_( 'No customers available' ) . '</td></tr>';
	}
	?>
	</table>
	<table cellspacing="0" cellpadding="6" border="0" width="100%" class="list">
	<tr class="<?=($i % 2 != 0) ? "second" : "first" ?>" >
		<td colspan="100%"><?=$pagination->create_links();?></td>
	</tr>
	</table>
	<?
/*}*/?>
<script>
	Calendar.enabled = true;

	function showCompany(id)
	{
		location.href = "./?m=companies&a=view&company_id="+id;
	}

	Table("idCompanyList").setNormalHover(1);

</script>
<form name="MdFormDelete" id="MdFormDelete" method="post">
	<input type="hidden" name="dosql" value ="do_delete" />
	<input type="hidden" name="m" value ="companies" />
	<input type="hidden" id="del_cc_id" name="company_id" value ="" />
	<input type="hidden" name="del" value ="" />
</form>